package com.ruoyi.deeplearn.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.deeplearn.domain.DeepModelVersion;
import com.ruoyi.deeplearn.mapper.ModelDeployVo;
import com.ruoyi.deeplearn.mapper.ModelForecastVo;
import com.ruoyi.deeplearn.mapper.ModelTrainVo;
import com.ruoyi.deeplearn.mapper.NewModelVersionVo;
import com.ruoyi.deeplearn.service.IDeepModelDatasetService;
import com.ruoyi.deeplearn.service.IDeepModelTrainService;
import com.ruoyi.deeplearn.service.IDeepModelVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.deeplearn.domain.DeepModel;
import com.ruoyi.deeplearn.service.IDeepModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模型Controller
 *
 * @author 任伟伟
 * @date 2022-12-10
 */
@RestController
@RequestMapping("/deeplearn/model")
public class DeepModelController extends BaseController
{
    @Autowired
    private IDeepModelService deepModelService;
    @Autowired
    private IDeepModelVersionService deepModelVersionService;
    @Autowired
    private IDeepModelTrainService deepModelTrainService;
    @Autowired
    private IDeepModelDatasetService deepModelDatasetService;
    /**
     * 查询模型列表
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeepModel deepModel)
    {
        startPage();
        List<DeepModel> list = deepModelService.selectDeepModelList(deepModel);
//        list.stream().filter(item->item.getCurrentVersionId()!=null).forEach(item->{
//            DeepModelVersion deepModelVersion = deepModelVersionService.
//                    selectDeepModelVersionById(item.getCurrentVersionId());
//            item.setCurrentVersion(deepModelVersion);
//        });
        return getDataTable(list);
    }

    @GetMapping("/list/all")
    public AjaxResult listAll(DeepModel deepModel)
    {
        List<DeepModel> list = deepModelService.selectDeepModelList(deepModel);
        return AjaxResult.success(list);
    }

    /**
     * 导出模型列表
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:export')")
    @Log(title = "模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeepModel deepModel)
    {
        List<DeepModel> list = deepModelService.selectDeepModelList(deepModel);
        ExcelUtil<DeepModel> util = new ExcelUtil<DeepModel>(DeepModel.class);
        util.exportExcel(response, list, "模型数据");
    }

    /**
     * 获取模型详细信息
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deepModelService.selectDeepModelById(id));
    }

    /**
     * 新增模型
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:add')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeepModel deepModel)
    {
        if(StringUtils.isEmpty(deepModel.getName())){
            return AjaxResult.error("模型编码不能为空");
        }
        if(StringUtils.isEmpty(deepModel.getCode())){
            return AjaxResult.error("模型编码不能为空");
        }
        //
//        DeepModel tmp = deepModelService.selectDeepModelByCode(deepModel.getCode());
//        if(tmp!=null){
//            return AjaxResult.error("模型编码已被占用,请输入其他编码");
//        }
        return toAjax(deepModelService.insertDeepModel(deepModel));
    }

    /**
     * 修改模型
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:edit')")
    @Log(title = "模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeepModel deepModel)
    {
        return toAjax(deepModelService.updateDeepModel(deepModel));
    }

    /**
     * 删除模型
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:remove')")
    @Log(title = "模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deepModelService.deleteDeepModelByIds(ids));
    }

    /////////////////////////////////////////////////////////
    //新增版本
    @PostMapping("/addNewVersion")
    public AjaxResult addNewVersion(@RequestBody NewModelVersionVo newModelVersionVo)
    {
        if(newModelVersionVo.getModel().getModuleNetId()==null){
            return AjaxResult.error("模型网络参数异常");
        }
        return toAjax(deepModelService.addNewVersion(newModelVersionVo));
    }
    /////////////////////////////////////////////////////////
    //模型配置训练集、测试集、算法
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:add')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping("/train")
    public AjaxResult addModelTrain(@RequestBody ModelTrainVo modelTrainVo)
    {
        return toAjax(deepModelService.addModelTrain(modelTrainVo));
    }

    //部署模型
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:deploy')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public AjaxResult addModelDeploy(@RequestBody ModelDeployVo modelDeployVo)
    {
        return toAjax(deepModelService.addModelDeploy(modelDeployVo));
    }

    //模型预测文件上传保存
//    @PreAuthorize("@ss.hasPermi('deeplearn:model:add')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping("/forecast")
    public AjaxResult addModelForecast(@RequestBody ModelForecastVo modelForecastVo)
    {
        return toAjax(deepModelService.addModelForecast(modelForecastVo));
    }


//    @PreAuthorize("@ss.hasPermi('deeplearn:model:forecast')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @GetMapping("/forecast/{forecastId}")
    public AjaxResult addModelForecast(@PathVariable("forecastId")Long forecastId)
    {
        return toAjax(deepModelService.addModelForecastByForecastId(forecastId));
    }
}
